package com.yxuan.springboot.service.impl;

import com.google.gson.Gson;
import com.yxuan.springboot.CustomizedException.DefaultException;
import com.yxuan.springboot.controller.DouyinPayController;
import com.yxuan.springboot.service.DyPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class DyPayServiceImpl implements DyPayService {

    public String imgUrl="https://volunteer-1318337852.cos.ap-nanjing.myqcloud.com/1/865071687779536275.jpg";

    @Resource(name = "wxPayClient")
    private CloseableHttpClient dyPayClient;


    @Override
    public void orderSynchronization(String access_token,String openId,String order_detail,Integer order_status)  {


        Map<String, Object> parame = new LinkedHashMap<>();
        //抖音小程序app
        parame.put("access_token", access_token);
        // 商户订单编号
        parame.put("app_name", "douyin");

        parame.put("open_id", openId);        //小程序用户的 open_id，通过 code2Session 获取


        parame.put("order_detail", order_detail);

        parame.put("order_status", order_status);      //普通小程序订单订单状态，POI 订单可以忽略, 0：待支付

        parame.put("order_type", 0);    //0：普通小程序订单（非POI订单）

        long timestamp = System.currentTimeMillis();
        parame.put("update_time", timestamp);

        try {
            Map getpost = DouyinPayController.getDSpost(parame, dyPayClient);
            log.info("订单同步的结果："+getpost);
        } catch (DefaultException e) {
            e.printStackTrace();
        }


    }

    @Override
    public String order_detail(String orderID, long create_time, String status, long total_price, List<Map<String,Object>> item_list) {

        Map<String, Object> parame = new LinkedHashMap<>();

        parame.put("order_id", orderID);
        // 商户订单编号
        parame.put("create_time", create_time);

        parame.put("status", status);

        parame.put("amount", 1L);

        parame.put("total_price", total_price);

        parame.put("detail_url", "pages/order/order");

        parame.put("item_list", item_list);

        Gson gson = new Gson();
        String json = gson.toJson(parame);
        log.info(json);
        return json;


    }


}
